import java.net.SocketTimeoutException;

public class LayerTwoClient {
	
	//Constructor
	public LayerTwoClient(){
		
	}

	public boolean start(String from_network_layer) throws Exception{

		LayerOneClient physical = new LayerOneClient();

		String data = "";
		String packet = "";
		String sequenceNumber = "0";
		int count = 0;

		boolean retry = false;

		while(from_network_layer!=null){

			if(retry == false){
				data = from_network_layer;
				packet = sequenceNumber+data;
			}

			physical.clientSend(packet);
		
			System.out.println("Package sent: "+packet.substring(1, packet.length()));
			System.out.println("Sequence number sent: "+packet.charAt(0));

			String ack = "";
			
			float start = System.currentTimeMillis();
			float end = start + 5*1000;

			while (end < System.currentTimeMillis()){	
				ack = physical.clientReceived();
				retry = false;
			}
			
			if (ack == sequenceNumber)
				count++;
			else{
				System.out.println("Timed out: considering package lost, retrying...");
				retry = true;
			}
		}
			
	}	

}
